﻿ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using UnityEngine.SceneManagement;

public class StartMenu : MonoBehaviour
{
    //背景视频
    private VideoPlayer video;
    //跳过按钮
    private GameObject objJump;
    //按钮组件
    private Button btnJump;
    //开始按钮
    private GameObject objStar;
    //开始组件
    private Button btnStar;
    //标题按钮
    private GameObject objTitle;

    public Text textIE;
    public string str = "科普时光：左脑和右脑的区别，主要在于神经功能方面的区别，左脑指的是左侧大脑半球，顾名思义，右脑指的是右侧大脑半球。左侧的大脑半球主要管理和支配右侧肢体和右侧面部肌肉运动感觉。而右侧大脑半球主要管理和支配左侧肢体和左侧面部的肌肉的运动感觉。左侧大脑半球主要管理逻辑推理运算等高级神经功能。而右侧大脑半球主要管理艺术音乐美术等功能。";
    public string s = "";
    //打字音效
    AudioSource clip;

    private void Awake()
    {
        GameObject bg = GameObject.Find("背景动画");
        video = bg.GetComponent<VideoPlayer>();
        //查找禁用的物体
        objJump = GameObject.Find("Canvas").transform.Find("跳过").gameObject;
        //添加事件监听
        btnJump = objJump.GetComponent<Button>();
        btnJump.onClick.AddListener(delegate 
        {
            objJump.SetActive(false);
            bg.SetActive(false);
            PlayText();
        });
        objStar = GameObject.Find("Canvas").transform.Find("开始").gameObject;
        //添加事件监听
        btnStar = objStar.GetComponent<Button>();
        btnStar.onClick.AddListener(delegate
        {
            SceneManager.LoadScene("TwoMenu");
        });
        //获取标题
        objTitle = GameObject.Find("Canvas").transform.Find("Title").gameObject;
    }
    void Start()
    {
        //PlayText();
        clip = this.GetComponent<AudioSource>();
    }
    public void PlayText()
    {
        StartCoroutine(ShowText(str.Length));
    }
    IEnumerator ShowText(int strLength)
    {
        objTitle.SetActive(true);
        clip.Play();
        int i = 0;
        while (i < strLength)
        {            
            yield return new WaitForSeconds(0.2f);
            s += str[i].ToString();
            textIE.text = s;
            if (s.Length == str.Length)
            {
                clip.Pause();
                objStar.SetActive(true);
            }
            i += 1;
        }
        StopAllCoroutines();
    }

    private void Update()
    {
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            video.Pause();
            objJump.SetActive(true);
        }
        if(Input.GetKeyUp(KeyCode.Backspace))
        {
            SceneManager.LoadScene("TwoMenu");
        }
    }
}
